const ranges = [
    { divider: Math.pow(1024, 9), suffix: 'B' },
    { divider: Math.pow(1024, 8), suffix: 'Y' },
    { divider: Math.pow(1024, 7), suffix: 'Z' },
    { divider: Math.pow(1024, 6), suffix: 'E' },
    { divider: Math.pow(1024, 5), suffix: 'P' },
    { divider: Math.pow(1024, 4), suffix: 'T' },
    { divider: Math.pow(1024, 3), suffix: 'G' },
    { divider: Math.pow(1024, 2), suffix: 'M' },
    { divider: Math.pow(1024, 1), suffix: 'K' }
];
const fixed = 2;

/**
 * 将数值转换:
 *     kilo   10e+3  K
 *     Mega   10e+6  M
 *     Giga   10e+9  G
 *     Tera   10e+12 T
 *     Peta   10e+15 P
 *     Exa    10e+18 E
 *     ...
 * 字符串格式的数值会被自动转换为数值， 如果转换失败则显示 NaN
 */
export const flowTransform = (n, unit= 'B') => {
    for (let i = 0; i < ranges.length; i++) {
        let approximate = n;
        if (ranges[i + 1]) {
            // 防止四舍五入后出现 1024.00M 这种情况
            approximate = (n / ranges[i + 1].divider).toFixed(fixed);
        }
        if (n >= ranges[i].divider || Number(approximate) >= 1024) {
            return (n / ranges[i].divider).toFixed(fixed) + ranges[i].suffix + unit;
        }
    }
    return n.toString() + unit;
};
